/**
 * 学生闯关留存数据分析
 *
 * created by qiuwei
 * 2016年05月19日
 */

'use strict';

const builder = require('../filterBuilder');
const api = require('../dicAPIUrl');

function request(requestData) {
	let timeUnit = requestData.currentTimeUnit;
	let cycle = requestData.cycle;
	let condition = requestData.condition;
	let filters = filter(condition);

	return  {
		"data": {
			"target": "hurdle_record-student_id"
		},
		"time_unit": timeUnit,
		"limit": +cycle + 1,
		"range": {
			"begin": condition.startDate,
			"end": condition.endDate
		},
		"filter": [].concat(filters),
		"basic_type": "hurdle_student_ext"
	}
}

function response() {
	let retainedData = Array.from(arguments);
	let rn = {
		retainedCountData: [],
		retainedRateData: [],
		summary: []
	};

	let map = retainedData.reduce((map, item) => {
		if (!map[item.multi_data])
			map[item.multi_data] = [];
		
		map[item.multi_data].push({
			// 留存数
			count: item.result,
			// 留存率
			rate: `${item.rate}%`,
			time: item.time_sheet
		});

		return map;
	}, {});

	for (let key in map) {
		let countRow = [key], rateRow = [key], summaryRow = [key];

		for (let i = 0, item = map[key][i]; item; item = map[key][++i]) {
			countRow.push(item.count);
			rateRow.push(item.rate);
			summaryRow.push({count: item.count, rate: item.rate});
		}

		rn.retainedCountData.push(countRow);
		rn.retainedRateData.push(rateRow);
		rn.summary.push(summaryRow);
	}

	return rn;
}

function filter(condition) {
	let filters = [];

	if (condition.province && condition.province != builder.NIL) {
		filters.push(builder.append('student-province_id', '=', condition.province));
	}
	if (condition.city && condition.city != builder.NIL) {
		filters.push(builder.append('student-city_id', '=', condition.city));
	}
	if (condition.county && condition.county != builder.NIL) {
		filters.push(builder.append('student-county_id', '=', condition.county));
	}
	if (condition.school && condition.school != builder.NIL) {
		filters.push(builder.append('student-school_id', '=', condition.school));
	}
	if (condition.stage[0] != builder.NIL) {
		filters.push(builder.append('student-grade_part', 'in', condition.stage.join(',')));
	}
	if (condition.grade[0] != builder.NIL) {
		filters.push(builder.append('student-grade', 'in', condition.grade.join(',')));
	}
	if (condition.subject[0] != builder.NIL) {
		filters.push(builder.append('hurdle_student_ext-hurdle_subject', 'in', condition.subject.join(',')));
	}
	if (condition.version != '0') {
		filters.push(builder.append('student-version', '=', condition.version));
	}
	if (condition.deviceType[0] != builder.NIL) {
		filters.push(builder.append('student-source', 'in', condition.deviceType.join(',')));
	}

	// filters.push(builder.append('homeworkanswer-day', '>=', condition.startDate));
	// filters.push(builder.append('homeworkanswer-day', '<=', condition.endDate));
	filters.push(builder.append('student-teacher_student_count', '>=', condition.userFeature));
	// filters.push(builder.append('student-teacher_student_count', '<=', condition.userFeature.end));

	return filters;
}

module.exports = {
	API_URL: api.API_GET_RETAIN_DATA,
	request,
	response
};